package com.jinyi.wwdeng.autodialer.utils.event

import android.content.Intent

/**
 * @Author wwdeng
 * @Annotation：事件发布者
 */
class EventPublisher {
    private var subscriberList = mutableListOf<EventSubscriber>()

    /**
     * 注册订阅者
     */
    fun register(subscriber: EventSubscriber){
        var isAdd = true
        for (i in 0 until subscriberList.size){
            if (subscriberList[i] == subscriber){
                isAdd = false
                break
            }
        }
        if (isAdd){
            subscriberList.add(subscriber)
        }
    }

    /**
     * 发布信息
     */
    fun publish(intent: Intent){
        subscriberList.forEach {
            it.publishEvent(intent)
        }
    }

    /**
     * 移除订阅者
     */
    fun detach(subscriber: EventSubscriber){
        subscriberList.remove(subscriber)
    }
}